#include <iostream>
#include <vector>
#include <string>
#include <unistd.h>
#include <pthread.h>
#define NUM 5
using namespace std;
int cnt=1000;
pthread_mutex_t glock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t gcond=PTHREAD_COND_INITIALIZER;
void* threadrun(void*args)
{
    string name =static_cast<const char*>(args);
    while(1)
    {
        pthread_mutex_lock(&glock);
        pthread_cond_wait(&gcond,&glock);
        cout<<name<<"计算"<<cnt<<endl;
        cnt++;
        pthread_mutex_unlock(&glock);
    }
}

int main()
{
    vector<pthread_t>threads;
    for(int i=0;i<NUM;i++)
    {
        pthread_t tid;
        char* name=new char[64];
        snprintf(name,64,"thread-%d",i);
        int n=pthread_create(&tid,nullptr,threadrun,name);
        if(n !=0)
        {
            continue;
        }
        threads.push_back(tid);
        sleep(1);
    }
    sleep(3);
    while(true)
    {
        cout<<"唤醒所有线程"<<endl;
        pthread_cond_broadcast(&gcond);
        sleep(1);
    }
    for(auto& id:threads)
    {
        pthread_join(id,nullptr);
    }
    return 0;
}   